package net.xxcl.kygl.service;

import java.util.List;
import net.xxcl.kygl.mapper.JtpsyjMapper;
import net.xxcl.kygl.mapper.ProjectMapper;
import net.xxcl.kygl.model.ProjectForJtpsyj;
import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.Jtpsyj;
import net.xxcl.kygl.pojo.JtpsyjKey;
import net.xxcl.kygl.pojo.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JtpsyjService {
    @Autowired
    private JtpsyjMapper jtpsyjMapper;
    @Autowired
    private ProjectMapper projectMapper;

    private static final Logger logger = LoggerFactory.getLogger(JtpsyjService.class);

    public int countByExample(Criteria example) {
        int count = this.jtpsyjMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public Jtpsyj selectByPrimaryKey(JtpsyjKey key) {
        return this.jtpsyjMapper.selectByPrimaryKey(key);
    }

    public List<Jtpsyj> selectByExample(Criteria example) {
        return this.jtpsyjMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(JtpsyjKey key) {
        return this.jtpsyjMapper.deleteByPrimaryKey(key);
    }

    public int updateByPrimaryKeySelective(Jtpsyj record) {
        return this.jtpsyjMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Jtpsyj record) {
        return this.jtpsyjMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.jtpsyjMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Jtpsyj record, Criteria example) {
        return this.jtpsyjMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Jtpsyj record, Criteria example) {
        return this.jtpsyjMapper.updateByExample(record, example);
    }

    public int insert(Jtpsyj record) {
        return this.jtpsyjMapper.insert(record);
    }

    public int insertSelective(Jtpsyj record) {
        return this.jtpsyjMapper.insertSelective(record);
    }

    /**
     * 指派专家
     * @param expertids
     * @param projectid
     * @return
     */
    public int appointExperts(String expertids,Integer projectid){
        Criteria criteria = new Criteria();
        criteria.put("projectid",projectid);
        //先把之前的删除
        jtpsyjMapper.deleteByExample(criteria);
        Jtpsyj jtpsyj = new Jtpsyj();
        jtpsyj.setProjectid(projectid);
        String[] str = expertids.split(",");
        for(int i=0;i<str.length;i++){
            //第一个为专家组组长
            if(i==0){
                jtpsyj.setIsleader(true);
            } else {
                jtpsyj.setIsleader(false);
            }
            jtpsyj.setUserid(Integer.parseInt(str[i]));
            jtpsyjMapper.insertSelective(jtpsyj);
        }
        Project project = new Project();
        project.setId(projectid);
        //指派专家成功，状态改变
        project.setState(9);
        projectMapper.updateByPrimaryKeySelective(project);
        return str.length;
    }

    public int countByJtpsyj(Criteria example) {
        int count = this.projectMapper.countByJtpsyj(example);
        logger.debug("count: {}", count);
        return count;
    }

    public List<Project> selectByJtpsyj(Criteria example) {
        return this.projectMapper.selectByJtpsyj(example);
    }
}